SELECT 
o.order_id orders_id,
o.customer_id customers_id, 
concat(o.firstname, ' ', o.lastname) customers_name, 
a.company customers_company, 
a.address_1 customers_street_address, 
a.address_2 customers_suburb, 
a.city customers_city, 
a.postcode customers_postcode, 
'' customers_state, 
a.country_id customers_country, 
o.telephone customers_telephone, 
o.email customers_email_address, 
1 customers_address_format_id, 
concat(o.shipping_firstname, ' ', o.shipping_lastname) delivery_name, 
o.shipping_company delivery_company, 
o.shipping_address_1 delivery_street_address, 
o.shipping_address_2 delivery_suburb, 
o.shipping_city delivery_city, 
o.shipping_postcode delivery_postcode, 
'' delivery_state, 
o.shipping_country delivery_country, 
o.shipping_address_format delivery_address_format_id, 
concat(o.payment_firstname, ' ', o.payment_lastname) billing_name, 
o.payment_company billing_company, 
o.payment_address_1 billing_street_address, 
o.payment_address_2 billing_suburb, 
o.payment_city billing_city, 
o.payment_postcode billing_postcode, 
'' billing_state, 
o.payment_country billing_country, 
o.payment_address_format billing_address_format_id, 
o.payment_method, 
o.date_modified last_modified, 
o.date_added date_purchased, 
o.order_status_id orders_status, 
o.currency_id currency, 
o.currency_value

FROM 
`<@PREFIX>order` o left join 
<@PREFIX>customer c on (c.customer_id = o.customer_id) left join 
<@PREFIX>address a on (c.address_id = a.address_id)

where 
o.store_id = <@PROCESS@>@select trim(substring ( imp_extid from 7 for 200)) from 
 IMP_LINK b join imp_source_extra a on a.imp_sourceid = b.imp_sourceid and 
a.wshopid = b.Wgroupid  and a.imp_sourceid = <@SOURCEID@> ##
((o.date_added >= %s and o.date_modified is null) or o.date_modified >= %s ) and 
o.order_status_id in (%s) and 
((o.date_added <= %s and o.date_modified is null) or o.date_modified <= %s )

order by 
o.date_added
